package org.simpleProxy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;



public class BasicConfigurator {
	
	//Use these to hold values
	public String ftpFolder;
	public int ftpPort;
	static Logger logger = Logger.getLogger(BasicConfigurator.class.getName());
	
	public BasicConfigurator(String configFileName) {
		
		String strLine = null;
		String[] lineArgs = new String[2];
		//Read the file if it exists, getting the following properties
		try {
			
			BufferedReader in = new BufferedReader(new FileReader(new File(configFileName)));
			
			//Parse the file to read in the configuration
			while((strLine = in.readLine()) != null){
				if(!strLine.substring(0, 1).equals("#")){
					lineArgs = strLine.split("=");
					
					if(lineArgs[0].equals("ftpFolder")){
						logger.info("Setting ftpfolder to " + lineArgs[1]);
						ftpFolder = lineArgs[1];
					}
					else if(lineArgs[0].equals("port")){
						logger.info("Setting port to " + lineArgs[1]);
						ftpPort = Integer.parseInt(lineArgs[1]);
					}
					else {
						logger.warn("Invalid property: " + lineArgs[0] + "- ignoring" );
					}
				}
			}
			in.close();
			
		//Otherwise use the defaults
		} catch (IOException e) {
			logger.warn("Could not find configuration file: " + configFileName + ", using default settings");
			ftpFolder = "ftproot";
			ftpPort = 2221;
		} catch (NullPointerException e){
			logger.warn("Could not find configuration file: " + configFileName + ", using default settings");
			ftpFolder = "ftproot";
			ftpPort = 2221;
		}
		
		
		
	}
}
